package com.zq.learn.springbatch.demo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.json.JacksonJsonObjectMarshaller;
import org.springframework.batch.item.json.JacksonJsonObjectReader;
import org.springframework.batch.item.json.JsonFileItemWriter;
import org.springframework.batch.item.json.JsonItemReader;
import org.springframework.batch.item.json.builder.JsonFileItemWriterBuilder;
import org.springframework.batch.item.json.builder.JsonItemReaderBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;

/**
 * JobDemoConfig
 *
 * @author ZhangQi
 * @date 2020/2/14
 */
@Configuration
@EnableBatchProcessing
public class JobDemoConfig {
  Logger logger= LoggerFactory.getLogger(JobDemoConfig.class);

  @Autowired
  private JobBuilderFactory jobBuilderFactory;
  @Autowired
  private StepBuilderFactory stepBuilderFactory;

  @Bean
  public JsonItemReader<DemoPojo> jsonItemReader() {
    logger.info("load JsonItemReader");
    return new JsonItemReaderBuilder<DemoPojo>()
      //定义读取的对象类型
      .jsonObjectReader(new JacksonJsonObjectReader<>(DemoPojo.class))
      //指定读取数据源
      .resource(new ClassPathResource("data.json"))
      //命名
      .name("jsonItemReader")
      .build();
  }
  @Bean
  public ItemProcessor<DemoPojo,DemoPojo> jsonItemProcessor(){
    logger.info("load ItemProcessor");
    //遍历每个对象
    return objects -> {
      logger.info("+++++++++++++++++++++Json Data:"+ JSON.toJSONString(objects));
      return objects;
    };
  }
  @Bean
  public JsonFileItemWriter<DemoPojo> jsonItemWriter() {
    logger.info("load JsonFileItemWriter");
    //指定文件。FileSystemResource有读、写操作，ClassPathResource有读、复制操作。
    FileSystemResource out = new FileSystemResource("src/main/resources/data_out.json");
    return new JsonFileItemWriterBuilder<DemoPojo>()
      //指定序列化方式
      .jsonObjectMarshaller(new JacksonJsonObjectMarshaller<>())
      //指定写入的文件
      .resource(out)
      //命名
      .name("jsonFileItemWriter")
      .build();
  }


  @Bean
  public Step demoStep(JsonItemReader<DemoPojo> reader,JsonFileItemWriter<DemoPojo> writer) {
    logger.info("load Step");
    return this.stepBuilderFactory.get("demoStep")
      //指定每块容量
      .<DemoPojo, DemoPojo>chunk(10)
      //指定读
      .reader(reader)
      //指定处理
      .processor(jsonItemProcessor())
      //指定写
      .writer(writer)
      .build();
  }

  @Bean
  public Job demoJob(Step demoStep) {
    logger.info("load Job");
    return this.jobBuilderFactory.get("demoJob")
      .start(demoStep)
      .build();
  }
}
